var api = require('../../../utils/api.js')
var $ = require('../../../utils/util.js')
var regeneratorRuntime = require('../../../utils/runtime.js')
var app = getApp()
var Store = app.Store

Page({
  data: {
    form:{}, // redux传过来的参数
    pageList: ['', '',], // 页面要展示的数据
    list: [],  
    result: [], // 当前选中的select [id] 
    type1_name_arr: [
      { id: 'dept_id', name:'dept_name'},
      { id: 'dept_id', name: 'dept_name' },
      { id: 'user_id', name: 'user_name' },
      { id: 'YCXLM', name: 'YCXLM' }
    ], 
    type1_name:'', // 名字字段
    type1_id:'', // id字段
    type2_name:'', // 名字字段
    type2_id:'', // 名字字段
    type3_id:'', // id字段
    type3_name_arr: [
      { id: 'dept_id', name: 'dept_name' },
      { id: 'dept_id', name: 'dept_name' },
      { id: 'user_id', name: 'user_name' },
      { id: 'YCXLM', name: 'YCXLM' }
    ],
    type3_name: '', // 名字字段
    type3_id: '', // id字段

    type3ActiveNames:['0'], // 
    radio3Item: {}, // 选择3的radio
    isShow:false,
    iptName:'',
    iptLabel:'',
    iptStation:'',
    iptLockCode:''
  },
  onLoad: function (options) {
    this.setData({
      form: Store.getState().formInfor.form
    })  
    
    switch (this.data.form.key){
      case 'selectTickDpt': // 获取两票部门信息  
      // 设置这两个字段的目的是方便标签列表读取属性渲染页面
        this.setData({
          type1_name:'areaName',
          type1_id:'areaId',
        })
        this._selectTickDpt()
        break;
      case 'selectWorkTeam':
        this.setData({
          type1_name: 'teamName',
          type1_id: 'teamId',
        })
        this._selectWorkTeam()
        break;
      case 'selectWorkPeops':
        this.setData({
          type2_name: 'userName',
          type2_id:'userId',
        })
        this._selectWorkPeops()
        break;
      case 'selectWorkProject':
        this.setData({
          type1_name: 'label',
          type1_id: 'id',
        })
        this._selectWorkProject()
        break;
      case 'selectWorkStation':
        this.setData({
          type1_name: 'label',
          type1_id: 'station',
        })
        this._selectWorkStation()
        break;
      case 'selectWorkDevice': // 获取设备名称 
        this.setData({
          type1_name: 'label',
          type1_id: 'id',
        })
        this._selectWorkDevice()
        break;
      case 'selectWorkSwitch': // 获取开关名称 
        this.setData({
          type1_name: 'label',
          type1_id: 'id',
        })
        this._selectWorkSwitch()
        break;
      case 'selectWorkType':
        this._selectWorkType()
        break;
      case 'selectWorkRisk':
        this._selectWorkRisk()
        break;
      case 'selectIssuer':
        this.setData({
          type1_name:'userName',
          type1_id:'userId',
        })
        this._selectIssuer()
        break;
      case 'selectWorkLeader':
        this.setData({
          type1_name:'userName',
          type1_id:'userId',
        })
        this._selectWorkLeader()
        break;
      case 'selectIssuer2': 
        this.setData({
          type1_name:'userName',
          type1_id:'userId',
        })
        this._selectIssuer2()
        break;
      case 'selectDirector':
        this.setData({
          type1_name:'userName',
          type1_id:'userId',
        })
        this._selectDirector()
        break;
      case 'selectDuty':
        this.setData({
          type1_name:'userName',
          type1_id:'userId',
        })
        this._selectDuty()
        break;
      case 'selectSupervisor':
        this.setData({
          type1_name:'userName',
          type1_id:'userId',
        })
        this._selectSupervisor()
        break;
      case 'selectOrderPeop':
        this.setData({
          type1_name:'userName',
          type1_id:'userId',
        })
        this._selectOrderPeop()
        break;
      case 'selectOperator':
          this.setData({
            type1_name:'userName',
            type1_id:'userId',
          })
          this._selectOperator()
          break;
      case 'selectPerControl':
        this._selectPerControl()
        break;
      // 操作票  工作票截至---------
      //锁具开始-----------------
      case 'selectLockCompany':
        this.setData({
          type1_name: 'label',
          type1_id: 'id',
        })
        this._selectLockCompany()
        break;
      case 'selectLockProject':
        this.setData({
          type1_name: 'label',
          type1_id: 'id',
        })
        this._selectLockProject()
        break;
      case 'selectLockStation':
        this.setData({
          type1_name: 'label',
          type1_id: 'id',
        })
        this._selectLockStation()
        break;
      case 'selectLockArea':
        this.setData({
          type1_name: 'label',
          type1_id: 'id',
        })
        this._selectLockArea()
        break;
      case 'selectLockName':
        this.setData({
          type1_name: 'label',
          type1_id: 'lockCode',
        })
        this._selectLockName()
        break;
      //锁具结束-----------------
      case 'selectCompany': // 获取企业信息  
        this.setData({
          type1_name: this.data.type1_name_arr[0].name,
          type1_id: this.data.type1_name_arr[0].id,
        })
        this._selectCompany()
        break;
      case 'selectEnterprise': // 获取企业信息  
        this.setData({
          type1_name: this.data.type1_name_arr[0].name,
          type1_id: this.data.type1_name_arr[0].id,
        })
        this._selectEnterprise()
        break;
      case 'selectStation': // 站房名称
        this.setData({
          type1_name: this.data.type1_name_arr[1].name,
          type1_id: this.data.type1_name_arr[1].id,
        })
        this._selectStation()
        break;
      case 'getRepairer': // 获取抢修负责人、 抢修人员 、缺陷发现人
        this.setData({
          type1_name: this.data.type1_name_arr[2].name,
          type1_id: this.data.type1_name_arr[2].id,
        })
        this._getRepairer()
        break;
      case 'selectYcxlm': // 根据站房获取设备列表
        this.setData({
          type1_name: this.data.type1_name_arr[3].name,
          type1_id: this.data.type1_name_arr[3].id,
        })
        this._selectYcxlm()
        break;
      case 'getDefectStandard': // 根据站房获取设备列表
        // this.setData({
        //   type1_name: this.data.type1_name_arr[3].name,
        //   type1_id: this.data.type1_name_arr[3].id,
        // })
        this._getDefectStandard()
        break;
      case 'selected': // 企业和站房列表接口 
        this._selected()
        break;
      case 'selectTree': // 省市 场站
        this._selectTree()
        break;
      case 'selectSchoolTree': // 学校 楼号 房间号
        this._selectSchoolTree()
      break;
        
    }

  },
  onShow: function () {
    
  },
//  工作票，操作票---开始
_selectTickDpt() {// 获取部门列表
  api.$get(api.main.selectAreaList).then((res) => {
    this.setData({
      pageList: res.data
    })
  })
},
_selectWorkTeam(){
  api.$get(api.main.selectWorkTeam,{areaId:this.data.form.config.areaId}).then((res) => {
    this.setData({
      pageList: res.data
    })
  })
},
_selectWorkPeops(){
  api.$get(api.main.selectWorkTeam).then((res) => {
    this.setData({
      pageList: res.data.find((item)=>item.teamId==this.data.form.config.teamId).teamMemberList
    })
  })
},
_selectWorkProject(){
  api.$get(api.main.selectProject).then((res) => {
    this.setData({
      pageList: res.data
    })
  })
},
_selectWorkStation(){
  api.$get(api.main.selectStation,{parentId:this.data.form.config.parentId}).then((res) => {
    this.setData({
      pageList: res.data
    })
  })
},
 _selectWorkDevice(){
  api.$get(api.main.selectDevice, { stationId: this.data.form.   config.stationId }).then((res) => {
    this.setData({
      pageList: res.data
    })
  })
 },
 _selectWorkSwitch(){
  api.$get(api.main.selectSwitch, { deviceId: this.data.form.config.deviceId }).then((res) => {
    this.setData({
      pageList: res.data
    })
  })
 },
 _selectWorkLeader(){
  api.$get(api.workTicket.workLeaderList, { deptId: this.data.form.config.deptId }).then((res) => {
    this.setData({
      pageList: res.data
    })
  })
 },
 _selectIssuer(){
  api.$get(api.workTicket.workIssuerList, { deptId: this.data.form.config.deptId }).then((res) => {
    this.setData({
      pageList: res.data
    })
  })
 },
 _selectIssuer2(){
  api.$get(api.workTicket.externalIssuerList, { deptId: this.data.form.config.deptId }).then((res) => {
    this.setData({
      pageList: res.data
    })
  })
 },
 _selectDirector(){
  api.$get(api.operateTicket.dutyCenterLeaderList, { deptId: this.data.form.config.deptId }).then((res) => {
    this.setData({
      pageList: res.data
    })
  })
 },
 _selectDuty(){
  api.$get(api.operateTicket.dutyCenterDutyList, { deptId: this.data.form.config.deptId }).then((res) => {
    this.setData({
      pageList: res.data
    })
  })
 },
 _selectSupervisor(){
  api.$get(api.operateTicket.ticketGuardianList, { deptId: this.data.form.config.deptId }).then((res) => {
    this.setData({
      pageList: res.data
    })
  })
 },
 _selectOrderPeop(){
  api.$get(api.operateTicket.receiveIssuer, { deptId: this.data.form.config.deptId }).then((res) => {
    this.setData({
      pageList: res.data
    })
  })
 },
 _selectOperator(){
  api.$get(api.operateTicket.operationList, { deptId: this.data.form.config.deptId }).then((res) => {
    this.setData({
      pageList: res.data
    })
  })
 },
// 工作票，操作票---结束
// 风险控制卡---开始
_selectWorkType(){
  this.setData({
    pageList: ['安装','调试','检查','维护','检修','试验','清扫','动火作业','高处作业','起重作业','临时用电','库区作业','受限空间','土方开挖','基坑支护','脚手架搭拆','模板安装','其他']
  })
 },
 _selectWorkRisk(){
  this.setData({
    pageList: ['高处坠落','物体打击','火灾/爆炸','灼烫','坍塌','起重伤害','机械伤害','中毒/窒息','触电','淹溺','车辆伤害','设备损坏','其他伤害']
  })
 },
 _selectPerControl(){
  this.setData({
    pageList: ['作业区域控制','作业程序控制','交叉作业控制','安全防护措施','个人防护措施','安全专项控制']
  })
 },

// 风险控制卡---结束
//锁具------开始
_selectLockCompany(){
  this.setData({ iptLabel: '企业'});
  api.$get(api.lockManage.companyList).then((res) => {
    this.setData({
      pageList: res.data
    })
  })
},
_selectLockProject(){
  this.setData({ iptLabel: '项目'});
  api.$get(api.lockManage.projectList, { parentId: this.data.form.config.parentId}).then((res) => {
    this.setData({
      pageList: res.data
    })
  })
},
_selectLockStation(){
  this.setData({ iptLabel: '厂站'});
  api.$get(api.lockManage.stationList, { parentId: this.data.form.config.parentId}).then((res) => {
    this.setData({
      pageList: res.data
    })
  })
},
_selectLockArea(){
  this.setData({ iptLabel: '区域'});
  api.$get(api.lockManage.areaList, { parentId: this.data.form.config.parentId}).then((res) => {
    this.setData({
      pageList: res.data
    })
  })
},
_selectLockName(){  
  this.setData({ iptLabel: '锁具'});
  api.$get(api.lockManage.lockList, { parentId: this.data.form.config.parentId}).then((res) => {
    this.setData({
      pageList: res.data
    })
  })
},
showPopup(){
  this.setData({ isShow: true});
},
changeDesCont(e){
  console.log(e.detail.value)
  this.setData({iptName:e.detail.value})
},
changeStationNum(e){
  this.setData({iptStation:e.detail.value})
},
changeLockNum(e){
  this.setData({iptLockCode:e.detail.value})
},
onClose() {
  this.setData({ isShow: false });
},
onSure(){
  if(this.data.iptLabel=='企业'){
    api.$post(api.lockManage.createCompany,{
      deptName:this.data.iptName
    }).then((res) => {
      this._selectLockCompany()
    })
  }else if(this.data.iptLabel=='项目'){
    api.$post(api.lockManage.createProject,{
      deptName:this.data.iptName,
      parentId: this.data.form.config.parentId
    }).then((res) => {
      this._selectLockProject()
    })
  }else if(this.data.iptLabel=='厂站'){
    api.$post(api.lockManage.createStation,{
      deptName:this.data.iptName,
      parentId: this.data.form.config.parentId,
      stationId: this.data.iptStation
    }).then((res) => {
      this._selectLockStation()
    })
  }else if(this.data.iptLabel=='区域'){
    api.$post(api.lockManage.createArea,{
      deptName:this.data.iptName,
      parentId: this.data.form.config.parentId
    }).then((res) => {
      this._selectLockArea()
    })
  }else if(this.data.iptLabel=='锁具'){
    api.$post(api.lockManage.createLockAdd,{
      deptName:this.data.iptName,
      lockCode:this.data.iptLockCode,
      parentId: this.data.form.config.parentId
    }).then((res) => {
      this._selectLockName()
    })
  }
  this.setData({ isShow: false });
},
//锁具------结束
  _selectCompany() {// 获取企业列表
    api.$get(api.main.selectCompany).then((res) => {
      this.setData({
        pageList: res.data
      })
    })
  },
  _selectEnterprise(){//获取企业列表
    api.$get(api.main.selectEnterprise).then((res) => {
      this.setData({
        pageList: res.data
      })
    })
  },

  _selectStation() {// 获取站房
    api.$get(api.main.selectStation1, { dept_id: this.data.form.config.company_id ? this.data.form.config.company_id:'', company: this.data.form.config.company}).then((res) => {
      this.setData({
        pageList: res.data
      })
    })
  },

  _getRepairer() {// 获取抢修负责人、 抢修人员 、缺陷发现人  type： 1 获取负责人；2获取人员；3缺陷发现人
    api.$get(api.main.getRepairer, { type: this.data.form.config.type,dept_id:this.data.form.config.dept_id}).then((res) => {
      this.setData({
        pageList: res.data
      })
    })
  },

  _selectYcxlm() {//根据站房获取设备列表
    api.$get(api.main.selectYcxlm, { station: this.data.form.config.station }).then((res) => {
      this.setData({
        pageList: res.data
      })
    })
  },

  _getDefectStandard() {//根据站房获取设备列表
    api.$get(api.main.getDefectStandard).then((res) => {
      this.setData({
        pageList: res.data
      })
    })
  },
  
  _selected() {// 获取所有的企业和站房列表接口
    api.$get(api.main.selected).then((res) => {
      this.setData({
        pageList: res.data
      })
    })
  },
  _selectTree() {// 省市 场站树
    api.$get(api.main.selectTree).then((res) => {
      this.setData({
        pageList: res.data
      })
    })
  },
  _selectSchoolTree() {// 学校 宿舍 房间号
    api.$get(api.main.selectSchoolTree,{"user_id":wx.getStorageSync('user_id')}).then((res) => {
      this.setData({
        pageList: res.data
      })
    })
  },
  
  type1BackHandle(e){
    var { item} = $.detail(e)
    var selected = ''
    switch (this.data.form.key) {
      case 'selectTickDpt': // 获取两票部门信息   
        selected = item
        break; 
      default:
        selected = item
        break; 
    }

    Store.dispatch({
      type: 'SET_FROM',
      form: { 
        key: this.data.form.key, // 后台要的更改字段 
        selected,
        config: this.data.form.config
      },
    })
    $.linkurl('', 'back')
  },
  type2BackHandle(){
    let result=[]
    if(this.data.form.config.dataType=='object'){
      console.log(this.data.pageList,this.data.result)
      result = this.data.pageList.filter(obj => this.data.result.includes(obj[this.data.type2_id].toString()));
      console.log(result)
    }else{
      result=this.data.result
    }
    Store.dispatch({
      type: 'SET_FROM',
      form: { 
        key: this.data.form.key, // 后台要的更改字段 
        selected:result,
        config: this.data.form.config
      },
    })
    $.linkurl('', 'back')
  },
  onType3CollapseChange(event) {
    this.setData({
      type3ActiveNames: event.detail
    });
  },
  onType3CollapseChange1(event) { 
    this.setData({
      type3ActiveNames1: event.detail
    });
  },

  onType3RadioChange(e){  
    var idType = ''
    switch (this.data.form.key) {
      case 'getDefectStandard':  // 缺陷性质
        idType = 'id'
        break
      case 'selected': // 企业和站房列表接口
      case 'selectTree':
      case 'selectSchoolTree':
        idType = 'dept_id'
        break
        
    } 
    this.setData({
      radio3Item: e.detail
    })
    var id = e.detail 
    var selected = null
    var pageList = this.data.pageList
    // try { 
    //   pageList.forEach((o_item, o_index)=>{ // 这么做的原因是 radio 只能是一个值
    //     if(o_item.list!=null)
    //     o_item.list.forEach((t_item, t_index)=>{
    //       if (t_item[idType] == id ){
    //         selected = t_item
    //         throw new Error('找不到元素了')
    //       }
    //     })
    //   })
    // }
    // catch (e){

    // } 
    try { 
      
      pageList.forEach((o_item, o_index)=>{ // 这么做的原因是 radio 只能是一个值
        if(o_item[idType]==id  ){
            selected = o_item
            throw new Error('跳出')
        }

        if(o_item.list)
        o_item.list.forEach((t_item, t_index)=>{    
          if (t_item[idType] == id  ){
            selected = t_item
            throw new Error('跳出')
          }
          if(t_item.list)
          t_item.list.forEach((s_item, s_index)=>{
            if (s_item[idType] == id  ){
              selected = s_item
              throw new Error('跳出')
            }
          })
        })
      })
    }
    catch (e){
    } 
    Store.dispatch({
      type: 'SET_FROM',
      form: {
        key: this.data.form.key, // 后台要的更改字段 
        selected,
      },
    })
    $.linkurl('', 'back')  
  },
  onType3RadioChange1(e){  
    var idType = ''
    switch (this.data.form.key) {
      case 'getDefectStandard':  // 缺陷性质
        idType = 'id'
        break
      case 'selected': // 企业和站房列表接口
      case 'selectTree':
      case 'selectSchoolTree':
        idType = 'dept_id'
        break
    } 
    this.setData({
      radio3Item: e.detail
    })
    var id = e.currentTarget.id
    var name= e.currentTarget.dataset.name
    var selected = null
    var pageList = this.data.pageList
    try { 
      pageList.forEach((o_item, o_index)=>{ // 这么做的原因是 radio 只能是一个值
        if(o_item[idType]==id && o_item['dept_name']==name ){
            selected = o_item
            throw new Error('跳出')
        }
        if(o_item.list)
        o_item.list.forEach((t_item, t_index)=>{
          if (t_item[idType] == id  && t_item['dept_name']==name ){
            selected = t_item
            throw new Error('跳出')
          }
          if(t_item.list)
          t_item.list.forEach((s_item, s_index)=>{
            if (s_item[idType] == id && s_item['dept_name']==name ){
              selected = s_item
              throw new Error('跳出')
            }
          })
        })

      })
    }
    catch (e){
    } 
    console.log('chajhshs',selected);
    Store.dispatch({
      type: 'SET_FROM',
      form: {
        key: this.data.form.key, // 后台要的更改字段 
        selected,
      },
    })
    $.linkurl('', 'back')  
  },
  onType3RadioChange2(e){  
    
    // console.log(e)

    var idType = ''
    switch (this.data.form.key) {
      case 'getDefectStandard':  // 缺陷性质
        idType = 'id'
        break
      case 'selected': // 企业和站房列表接口
        idType = 'dept_id'
        break
    } 
    this.setData({
      radio3Item: e.currentTarget.id
    })
    var id = e.currentTarget.id
    var selected = null
    var pageList = this.data.pageList
    // console.log(id);
    try { 
      
      pageList.forEach((o_item, o_index)=>{ // 这么做的原因是 radio 只能是一个值
        if(o_item[idType]==id){
            selected = o_item
            throw new Error('跳出')
        }

        if(o_item.list)
        o_item.list.forEach((t_item, t_index)=>{
          
      
          if (t_item[idType] == id ){
            selected = t_item
            throw new Error('跳出')
          }
          if(t_item.list)
          t_item.list.forEach((s_item, s_index)=>{
            if (s_item[idType] == id ){
              selected = s_item
              throw new Error('跳出')
            }
          })

        })

      })
    }
    catch (e){
    } 
    // console.log(selected);
    Store.dispatch({
      type: 'SET_FROM',
      form: {
        key: this.data.form.key, // 后台要的更改字段 
        selected,
      },
    })
    $.linkurl('', 'back')  
  },

  onChecboxChange(event) {
    console.log(event)
    this.setData({
      result: event.detail
    });
  },

  checboxToggle(event) {
    const { index } = event.currentTarget.dataset;
    const checkbox = this.selectComponent(`.checkboxes-${index}`);
    checkbox.toggle();
  },



})